package yzbx.personalwebsite.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import oshi.SystemInfo;
import oshi.hardware.CentralProcessor;
import oshi.hardware.GlobalMemory;
import oshi.software.os.OSFileStore;
import oshi.software.os.OperatingSystem;
import oshi.util.Util;
import yzbx.personalwebsite.config.ConfigData;
import yzbx.personalwebsite.dao.CommonDao;
import yzbx.personalwebsite.entityWeb.ServerStateResponse;
import yzbx.personalwebsite.tool.RedisTool;

import java.text.DecimalFormat;

/**
 * {@code @Author} zxm
 * {@code @Description} 获取系统信息
 * {@code @Date} 2024/11/12/14:44
 * {@code @Version} 1.0
 */
@Service
public class GetServerInfo {

    private static final Logger LOGGER = LoggerFactory.getLogger(GetServerInfo.class);
    // redis工具类
    protected final RedisTool redisTool;
    private final CommonDao commonDao;

    @Autowired
    public GetServerInfo(RedisTool redisTool, CommonDao commonDao, ConfigData configData) {
        this.redisTool = redisTool;
        this.commonDao = commonDao;
    }


    public ServerStateResponse getServerInfo() {
        SystemInfo si = new SystemInfo();
        OperatingSystem os = si.getOperatingSystem();
        CentralProcessor processor = si.getHardware().getProcessor();
        GlobalMemory memory = si.getHardware().getMemory();
        ServerStateResponse ssr = new ServerStateResponse();

        ssr.setPcOs(os.getFamily());
        ssr.setPcOsVersion(os.getVersionInfo().toString());
        ssr.setPcCpuName(processor.getProcessorIdentifier().getName());
        ssr.setPcCpuPPC(processor.getPhysicalProcessorCount() + "");
        ssr.setPcCpuLPC(processor.getLogicalProcessorCount() + "");
        long[] prevTicks = processor.getSystemCpuLoadTicks();
        Util.sleep(1000);
        long[] ticks = processor.getSystemCpuLoadTicks();
        long nice = ticks[CentralProcessor.TickType.NICE.getIndex()] - prevTicks[CentralProcessor.TickType.NICE.getIndex()];
        long irq = ticks[CentralProcessor.TickType.IRQ.getIndex()] - prevTicks[CentralProcessor.TickType.IRQ.getIndex()];
        long softirq = ticks[CentralProcessor.TickType.SOFTIRQ.getIndex()] - prevTicks[CentralProcessor.TickType.SOFTIRQ.getIndex()];
        long steal = ticks[CentralProcessor.TickType.STEAL.getIndex()] - prevTicks[CentralProcessor.TickType.STEAL.getIndex()];
        long cSys = ticks[CentralProcessor.TickType.SYSTEM.getIndex()] - prevTicks[CentralProcessor.TickType.SYSTEM.getIndex()];
        long user = ticks[CentralProcessor.TickType.USER.getIndex()] - prevTicks[CentralProcessor.TickType.USER.getIndex()];
        long iowait = ticks[CentralProcessor.TickType.IOWAIT.getIndex()] - prevTicks[CentralProcessor.TickType.IOWAIT.getIndex()];
        long idle = ticks[CentralProcessor.TickType.IDLE.getIndex()] - prevTicks[CentralProcessor.TickType.IDLE.getIndex()];
        long totalCpu = user + nice + cSys + idle + iowait + irq + softirq + steal;
        ssr.setPcCpuSysUseRate(new DecimalFormat("#.##%").format(cSys * 1.0 / totalCpu));
        ssr.setPcCpuUsrUseRate(new DecimalFormat("#.##%").format(user * 1.0 / totalCpu));
        ssr.setPcCpuWaitRate(new DecimalFormat("#.##%").format(iowait * 1.0 / totalCpu));
        ssr.setPcCpuFreeRate(new DecimalFormat("#.##%").format(idle * 1.0 / totalCpu));
        ssr.setPcMemoryTotal(byteToGb(memory.getTotal()));
        ssr.setPcMemoryUsed(byteToGb(memory.getTotal() - memory.getAvailable()));
        ssr.setPcMemoryFree(byteToGb(memory.getAvailable()));
        long PcDiskTotal = 0;
        long PcDiskFree = 0;
        for (OSFileStore fs : si.getOperatingSystem().getFileSystem().getFileStores()) {
            PcDiskTotal += fs.getTotalSpace();
            PcDiskFree += fs.getFreeSpace();
        }
        ssr.setPcDiskTotal(byteToGb(PcDiskTotal));
        ssr.setPcDiskUsed(byteToGb(PcDiskTotal - PcDiskFree));
        ssr.setPcDiskFree(byteToGb(PcDiskFree));

        ssr.setPcConnect("已连通");
        ssr.setMysqlConnect((commonDao.pingMysql() ? "已连通" : "未连通"));
        ssr.setRedisConnect((redisTool.pingRedis() ? "已连通" : "未连通"));

        return ssr;
    }

    public static void main(String[] args) throws InterruptedException {
//        // 创建 SystemInfo 对象
//        SystemInfo si = new SystemInfo();
//        // 获取操作系统信息
//        OperatingSystem os = si.getOperatingSystem();
//        System.out.println("操作系统: " + os.getFamily() + " " + os.getVersionInfo());
//        // 获取 CPU 信息
//        CentralProcessor processor = si.getHardware().getProcessor();
//        System.out.println("CPU: " + processor.getProcessorIdentifier().getName());
//        // 打印 CPU 的相关信息
//        System.out.println("CPU 核心数: " + processor.getPhysicalProcessorCount());
//        System.out.println("CPU 线程数: " + processor.getLogicalProcessorCount());
//        // CPU信息
//        long[] prevTicks = processor.getSystemCpuLoadTicks();
//        Util.sleep(1000);
//        long[] ticks = processor.getSystemCpuLoadTicks();
//        long nice = ticks[CentralProcessor.TickType.NICE.getIndex()] - prevTicks[CentralProcessor.TickType.NICE.getIndex()];
//        long irq = ticks[CentralProcessor.TickType.IRQ.getIndex()] - prevTicks[CentralProcessor.TickType.IRQ.getIndex()];
//        long softirq = ticks[CentralProcessor.TickType.SOFTIRQ.getIndex()] - prevTicks[CentralProcessor.TickType.SOFTIRQ.getIndex()];
//        long steal = ticks[CentralProcessor.TickType.STEAL.getIndex()] - prevTicks[CentralProcessor.TickType.STEAL.getIndex()];
//        long cSys = ticks[CentralProcessor.TickType.SYSTEM.getIndex()] - prevTicks[CentralProcessor.TickType.SYSTEM.getIndex()];
//        long user = ticks[CentralProcessor.TickType.USER.getIndex()] - prevTicks[CentralProcessor.TickType.USER.getIndex()];
//        long iowait = ticks[CentralProcessor.TickType.IOWAIT.getIndex()] - prevTicks[CentralProcessor.TickType.IOWAIT.getIndex()];
//        long idle = ticks[CentralProcessor.TickType.IDLE.getIndex()] - prevTicks[CentralProcessor.TickType.IDLE.getIndex()];
//        long totalCpu = user + nice + cSys + idle + iowait + irq + softirq + steal;
//
//        System.out.println("cpu系统使用率:" + new DecimalFormat("#.##%").format(cSys * 1.0 / totalCpu));
//        System.out.println("cpu用户使用率:" + new DecimalFormat("#.##%").format(user * 1.0 / totalCpu));
//        System.out.println("cpu当前等待率:" + new DecimalFormat("#.##%").format(iowait * 1.0 / totalCpu));
//        System.out.println("cpu当前空闲率:" + new DecimalFormat("#.##%").format(idle * 1.0 / totalCpu));
//
//        // 获取内存信息
//        GlobalMemory memory = si.getHardware().getMemory();
//        System.out.println("总量内存: " + byteToGb(memory.getTotal()));
//        System.out.println("已用内存: " + byteToGb(memory.getTotal() - memory.getAvailable()));
//        System.out.println("可用内存: " + byteToGb(memory.getAvailable()));
//        // 获取磁盘空间信息
//        for (OSFileStore fs : si.getOperatingSystem().getFileSystem().getFileStores()) {
//            System.out.println("磁盘: " + fs.getMount() + " 总空间: " + byteToGb(fs.getTotalSpace()) + " 剩余空间: " + byteToGb(fs.getFreeSpace()));
//        }

    }


    private static String byteToGb(long b) {
        return b * 100 / 1024 / 1024 / 1024 / 100.0 + " GB";
    }
}
